
using System; 
using System.Collections.Generic;
using UnityEngine;



namespace XFGameFramework
{

    // Fix 编码

    public class UpdateManager : MonoBehaviour
    {
        #region 事件
        /// <summary>
        /// Update时调用
        /// </summary>
        public static System.Action onUpdate;
        /// <summary>
        /// FixedUpdate时调用
        /// </summary>
        public static System.Action onFixedUpdate;
        /// <summary>
        /// LateUpdate时调用
        /// </summary>
        public static System.Action onLateUpdate;

        private static List<System.Action> list_update = new List<System.Action>();
        private static List<System.Action> list_fixedupdate = new List<System.Action>();
        private static List<System.Action> list_lateupdate = new List<System.Action>();

        private static bool running = false;

        #endregion

        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
        static void InitUpdateManager()
        {
            onUpdate = null;
            onFixedUpdate = null;
            onLateUpdate = null;

            foreach (var item in list_update)
                onUpdate += item;

            foreach (var item in list_fixedupdate)
                onFixedUpdate += item;
             
            foreach (var item in list_lateupdate)
                onLateUpdate += item;
             
            list_update.Clear();
            list_fixedupdate.Clear();
            list_lateupdate.Clear();

            GameObject obj = new GameObject("UpdateManager");
            DontDestroyOnLoad(obj);
            obj.AddComponent<UpdateManager>();
        }
  
        private void FixedUpdate()
        {
            onFixedUpdate?.Invoke();
        }

        private void Update()
        {
            onUpdate?.Invoke();
        }

        private void LateUpdate()
        {
            onLateUpdate?.Invoke();
        }


        public static void AddUpdateEvent(System.Action action) 
        {
            onUpdate += action;
            // 当UpdateManger还未初始化时，把委托暂存到集合中
            if(!running) list_update.Add(action); 
        }

        public static void AddFixedUpdateEvent(System.Action action) 
        {
            onFixedUpdate += action;
            // 当UpdateManger还未初始化时，把委托暂存到集合中
            if (!running) list_fixedupdate.Add(action);
        }

        public static void AddLateUpdateEvent(System.Action action) 
        {
            onLateUpdate += action;
            // 当UpdateManger还未初始化时，把委托暂存到集合中
            if (!running) list_lateupdate.Add(action);
        }

        public static void RemoveUpdateEvent(System.Action action) 
        {
            onUpdate -= action;
            list_update.Remove(action);
        }

        public static void RemoveFixedUpdateEvent(System.Action action)
        {
            onFixedUpdate -= action;
            list_fixedupdate.Remove(action);
        }

        public static void RemoveLateUpdateEvent(System.Action action)
        {
            onLateUpdate -= action;
            list_lateupdate.Remove(action);
        }

        private void OnEnable()
        {
            running = true;
        }

         

        private void OnDisable()
        {
            running = false;
            list_update.Clear();
            list_fixedupdate.Clear();
            list_lateupdate.Clear();
        }

    }
}

